package core

class CountryController   {
	
	static layout='general'
	
	
	def beforeInterceptor = [action: this.&checkId, except: ['index', 'create', 'save']]
	private checkId() {
		def countryID = params.long('id')
		if (!countryID || !Country.get(countryID)) {
			flash.error = "not a valid ID ${countryID}"
			redirect action:'index'
			return false
		}
	}
	//static layout = 'main'


	//static defaultAction = "create"
	//    def index() {
	//	//	method()
	//		render text: """Country.index
	//<pre>
	//            servletContext: ${servletContext['servletContextKey']}
	//            session: ${session.sessionKey}
	//            request: ${request.requestKey}
	//            params: ${params}
	//            flash: ${flash}
	//            ${link(action:'list', 'Go to list')}
	//            </pre>
	//     """
	//		      //render text: 'country.index'
	//	}
	//	def list = {
	//		servletContext['servletContextKey'] = 'servletContext Value'
	//		session['sessionKey'] = 'session Value'
	//		request['requestKey'] = 'request Value'
	//		flash['flashKey'] = 'flash Value'
	//		render text: """Country.list
	//     <pre>
	//          servletContext: ${servletContext.servletContextKey}
	//          session: ${session.sessionKey}
	//          request: ${request['requestKey']}
	//          params: ${params  }
	//		  flash: ${flash}
	//		  ${link(action:'index', 'Go to index')}
	//		  </pre>
	//		  """
	//
	//		//render text: 'country.list'
	//	}
	//	===========================================================
	//	def show() {
	//		render text: "Country.show"
	//	}
	//	private method () {
	//		println 'privateMethod'
	//		render text: 'prinvateMethod'
	//}
	//	============================================================

	// ===============Models and Views//Model Returning=============

	// explicit map instance is returned to view show.gsp
	//	def show() {
	//	[country: Country.get(params.long('id'))]
	//	}
	//	// Selecting the View
	//	def countries() {
	//	def map = [ countries: Country.list() ]
	//	render(view: "all", model: map)
	//	}
	//	// Selecting the shared View
	//	def palestine() {
	//	def map = [ ps: Country.findByCode('PS') ]
	//	render(view: "/shared/ps", model: map)
	//	}

	//	========================Rendering a Response===========================

	//	def rend() {
	//		def countryList = Country.list()
	//		// renders text to response
	//		render "some text"
	//	}



	//===========================Redirects============================================

	//	def show() {
	//	[country: Country.get(params.long('id'))]
	//	}
	//
	//	def redi() {
	//		// redirects to the list action within the same controller
	//		//redirect(action: list)
	//		//redirect(controller: 'Person', action: 'index')
	//		//redirect(action: 'show', params:[id:17])
	//		redirect(controller: "country", action: "list", fragment: "ps")

	//	=========================Groovy Server Pages GSP (V in the MVC)======Session 09==========


	def index () {
		def max = params.long('max')?:25
		def start = params.long('start')?:0
		[countries:Country.list(max: max, offset:start ), max:max ,start:start]

		//		def  imageUrl = g.createLinkTo(dir:"images", file:"grails_logo.png").toString()
		//			render "${imageUrl}"
	}

	def show (long id) {
		[country:Country.get(id)]

	}
	def create () {
		render view:'create'

	}
	def save() {
		def country = new Country(params)
		if (country.save()) {
			flash.notice = "Country ${country.code}: is saved successfuly"
			redirect(action:'index')
		} else {
			flash.error = "Country ${country.code}: Failed to save"
			render view:'create', model:[country:country]
		}

	}





}









